<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Http\Requests\Admin\BaseRequest;
use App\Models\Admin\CustomerWx;
use Illuminate\Validation\Rule;

class CustomerWxController extends AdminController
{
    use AdminTrait;

    protected $model;
    protected $unique_id='';

    protected $validate_rule = [
        'link' => 'required'
    ];

    protected $err_msg = [
        'name.required' => '姓名必须',
        'name.unique' => '姓名必须唯一',
        'mobile.regex' => '电话格式不对',
        'mobile.unique' => '电话必须唯一',
    ];

    protected function rules()
    {
        return [
            'name' => [
                'required',
                Rule::unique('admin_customer_wx')->ignore($this->unique_id),
            ],
            'mobile' => [
                'regex:/^1[23456789][0-9]{9}$/',
                Rule::unique('admin_customer_wx')->ignore($this->unique_id, 'id'),
            ],
        ];
    }

    public function __construct(CustomerWx $model)
    {
        $this->model = $model;
    }

    protected function search($request)
    {
        $select = $this->model;
        ($name = $request->query('name')) && $select = $select->where('name','like',"%$name%");
        return $select;
    }

    protected function setRequestField($arr,$type=false)
    {
        //过滤字段
        if (isset($arr['sign'])) unset($arr['sign']);
        if (isset($arr['timeStamp'])) unset($arr['timeStamp']);
        return $arr;
    }

    public function getCustListAll(BaseRequest $request)
    {
        $list = $this->model->select('id','name','link')->get();
        return $this->success(['list'=>$list]);
    }
}
